<?php
/*
 * @Date: 2021-01-28 23:31:24
 * @名称: 后台 · 漫画 · 修改漫画信息 · 读取漫画信息
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-02-02 22:15:41
 * @FilePath: /Site/app/useradmin/controller/Comics/Get/GetComicsInfo.php
*/

namespace app\useradmin\controller\Comics\Get;


use app\BaseController;
use think\facade\Db;

class GetComicsInfo extends BaseController
{
    /**
     * @方法名: 获取书籍信息
     * @作者: 初雪桜
     * @信息: 获取某个书籍信息 用于修改书籍使用
     * @param String $user_info 用户token解析用户信息
     * @param Int $time 时间戳
     * @param String $bookId 书籍id
     * @param String $hashHmac_bookId 加密后 加密id
     * @param String $hashHmac_toKen 加密后 加密id
     * @return Html
     */
    public function GetComicsInfo($user_info, $time, $bookId, $hashHmac_bookId, $hashHmac_toKen)
    {
        $book_info = Db::table('book_info')
            ->alias('book_info')
            ->join("book_list book_list", "book_list._id=book_info._id")
            ->where([
                "book_info._id" => $bookId,
            ])
            ->field([
                "book_list.title",
                #标题
                "book_list.original_name",
                #保存文件名字
                "book_list.path",
                #路径
                "book_list.file_server",
                #服务器地址
                "book_info.categories",
                #分类
                "book_info.tags",
                #标签
                "book_info.description",
                #简介
                "book_info.allow_download",
                #是否允许下载
                "book_info.allow_comment",
                #是否允许评论
            ])
            ->select(); //查询这个漫画的数据

        if (count($book_info) == 1) {
            $book_info = $book_info[0];
            $book_info['author_and_chinese_team'] = Db::table('author_and_chinese_team') //如果就一条数据 就开始查询作者
                ->alias('author_and_chinese_team')
                // ->leftJoin('user_info author', 'author._id = author_and_chinese_team.author_id')
                ->leftJoin('user_info user_info', 'user_info._id = author_and_chinese_team.user_id')
                ->where(
                    [
                        "author_and_chinese_team.book_id" => $bookId,
                        "author_and_chinese_team.delete_time" => null,
                    ]
                )
                ->field([
                    'author_and_chinese_team.author_or_chinese_team', //作者表 协作者1 / 创建者0
                    'author_and_chinese_team.state', //状态 0正常 1 新加入待漫画作者审核 2漫画作者审核未通过 3自己退出项目 4协作者请求删除协作者 5代漫画作者审核 6已删除
                    'author_and_chinese_team._id', //列表id
                    'author_and_chinese_team.exp_state', //用户权限信息 
                    'author_and_chinese_team.edit_delete_user', //是否允许删除用户 
                    'author_and_chinese_team.edit_inherit_users', //是否允许继承用户
                    'author_and_chinese_team.edit_add_user', //是否允许 新增用户
                    'author_and_chinese_team.edit_sever_comics_info', //是否允许 保存修改漫画信息
                    'author_and_chinese_team.is_deleted',//是否显示删除按钮
                    'user_info._id as user_id', //用户表 协作者1 / 创建者0
                    'user_info.user_name as user_name', //用户表 用户名
                ])
                ->select()->toArray();

            $book_info['me_role'] = 'other'; //初始化定义我的角色为其他
            foreach ($book_info['author_and_chinese_team'] as $key => $value) {
                if ($value['state'] == 1 && $value['user_id'] == $user_info["_id"]) {
                    switch ($value['exp_state']) {
                        case 8:
                            $errorCode = '104';
                            $errorMsg = '已自行提交用户权限';
                            break;
                        case 2:
                            $errorCode = '105';
                            $errorMsg = '已被漫画作者移除';
                            break;
                        case 7:
                            $errorCode = '106';
                            $errorMsg = '您自己已退出漫画项目';
                            break;
                        case 11:
                            $errorCode = '107';
                            $errorMsg = '您的用户待审核 请联系本文章创作者';
                            break;
                        default:
                            $errorCode = 'Null';
                            $errorMsg = '未知原因';
                            break;
                    }
                    return view('OtherPage/Error', [
                        "errorCode" => $errorCode,
                        "errorTitle" => "发生意外错误 错误提示 ⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️",
                        "errorMsg" => $errorMsg,
                    ]);
                    exit();
                }
                if ($value["author_or_chinese_team"] == 0) { //判断是不是作者
                    if ($value["user_id"] == $user_info['_id']) {
                        $book_info['me_role'] = 'author'; //如果我是作者 设置角色为作者
                        $book_info['edit_delete_user'] = $value['edit_delete_user']; //如果我是协作者 设置我为协作者
                        $book_info['edit_add_user'] = $value['edit_add_user']; //是否允许新增用户 是否允许新增用户
                        $book_info['edit_sever_comics_info'] = $value['edit_sever_comics_info']; //是否允许新增用户 是否允许新增用户

                    }
                    $book_info['author_and_chinese_team'][$key]['authorOrChinese_team'] = '创建者';
                } else if ($value["author_or_chinese_team"] == 1) { //判断是不是协作者
                    if ($value["user_id"] == $user_info['_id']) {
                        $book_info['me_role'] = 'chinese_team'; //如果我是协作者 设置我为协作者
                        $book_info['edit_delete_user'] = $value['edit_delete_user']; //如果我是协作者 设置我为协作者
                        $book_info['edit_add_user'] = $value['edit_add_user']; //是否允许新增用户 是否允许新增用户
                        $book_info['edit_sever_comics_info'] = $value['edit_sever_comics_info']; //是否允许新增用户 是否允许新增用户

                    }
                    $book_info['author_and_chinese_team'][$key]['authorOrChinese_team'] = '协作者';
                }

                $hash_hmac_json = [
                    "hash_hmac_user_id" => hash_hmac("sha256", $value["user_id"] . $bookId . $time, config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")), //书籍用户id 加密后 加密id
                    "user_id" => $value["user_id"],
                    "time" => $time,
                    "_id" => $value["_id"],
                    "book_id" => $bookId,
                    "role" => $value["author_or_chinese_team"],
                ];
                $book_info['author_and_chinese_team'][$key]['hash_hmac_str'] = authcode(base64_encode(json_encode(json_decode(json_encode($hash_hmac_json)))), 'encode', config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption"));
            }
            //解析漫画分类
            $categories = [];
            $classified_catalogueList = Db::table('classified_catalogue')
                ->select();

            $me_categories = array();
            foreach (explode(',', $book_info["categories"]) as $categoriesKey => $categoriesValue) {
                $me_categories[$categoriesValue] = true;
            }
            foreach ($classified_catalogueList as $key => $value) {
                $classified_catalogue[$key] = [
                    "name" => $value["title"],
                    "value" =>  $value["_id"],
                    "selected" => (array_key_exists($value["title"], $me_categories)) ? true : false,
                ];
            }
            foreach (explode(',', $book_info["categories"]) as $categoriesKey => $categoriesValue) {
                array_push($categories, [
                    "name" => "$categoriesValue",
                    "value" => '$categoriesValue',
                ]);
            }
            //解析漫画分类
            //!解析漫画标签
            $comicsTag = explode(',', $book_info["tags"]);

            $ArrayComicsTagList = [];
            #全部数组
            //!初始化全部数据 会减慢运行速度很多
            $TagList = Db::table('tags')
                ->where(['state' => 1])
                ->field(["tag_name"])
                ->select();
            foreach ($TagList as $key => $value) {
                $ArrayComicsTagList[$value['tag_name']] = [
                    "name" => $value['tag_name'],
                    "value" => $value['tag_name'],
                ];
            }
            $SelectComicsTagList = [];
            //以选择数组

            foreach ($comicsTag as $key => $value) {
                $SelectComicsTagList[$value] = [
                    "name" => $value,
                    "value" => $value,
                    "selected" => true,
                ];
            }

            foreach ($SelectComicsTagList as $selectKey => $selectValue) {
                $ArrayComicsTagList[$selectValue['name']] = [
                    "name" => $selectValue['name'],
                    "value" => $selectValue['name'],
                    "selected" => true,
                ];
            }
            $ListComicsTagList = [];
            foreach ($ArrayComicsTagList as $key => $value) {
                array_push($ListComicsTagList, $value);
            }
            //!解析漫画标签
            if ($book_info['me_role'] == 'other') { //判断我在这篇文章的角色 如果是其他 不是作者 也不是协作者
                return view('OtherPage/Error', [
                    "errorCode" => 2003,
                    "errorTitle" => "发生意外并且严重错误",
                    "errorMsg" => "不允许此用户操作 这是不允许的操作",
                ]);
            }
            return view('Edit/EditComicsInfo/EditComicsInfo', [
                "useradmin" => config("useradminConfig.useradmin_serverHost"),
                "bookId" => $bookId,
                "bookInfo" => $book_info,
                "time" => $time,
                "hashHmac_bookId" => $hashHmac_bookId,
                "hashHmac_toKen" => $hashHmac_toKen, //加密用户id 加密方法 token+书籍id+时间戳
                "classified_catalogue" => json_encode($classified_catalogue), //分类
                "TagList" => json_encode($ListComicsTagList), //分类
                "user_info" => $user_info,
            ]);
        } else {
            return view('OtherPage/Error', [
                "errorCode" => 2005,
                "errorTitle" => "发生严重错误",
                "errorMsg" => "这一个漫画id对应多个漫画请尝试修复 这是不允许的操作",
            ]);
        }
    }
}
